//
//  FindViewController.m
//  半糖
//
//  Created by RenShuang on 15-9-1.
//  Copyright (c) 2015年 shuang. All rights reserved.
//

#import "FindViewController.h"
#import "BaseHeader.h"

@interface FindViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *bScrollArr;
@property(nonatomic,retain)NSMutableArray *sScrollArr;
@property(nonatomic,retain)NSMutableArray *tableArr;
@property(nonatomic,retain)UIView *headerView;
@property(nonatomic,retain)UIScrollView *bScrollView;
@property(nonatomic,retain)UIScrollView *sScrollView;
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)NSInteger footer;

@end

@implementation FindViewController

- (void)dealloc
{
    [_myTableView release];
    [_sScrollView release];
    [_bScrollView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.headerView.backgroundColor = [NightHandle shareHandle].viewColor;
    self.sScrollView.backgroundColor = [NightHandle shareHandle].viewColor;
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"发现";
    //改变navigationBar上title的颜色,大小,字体等
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.6],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Marion-Italic" size:22.0],UITextAttributeFont,nil]];
    
    _bScrollArr = [[NSMutableArray alloc] init];
    _sScrollArr = [[NSMutableArray alloc] init];
    _tableArr = [[NSMutableArray alloc] init];
    _footer = 0;
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_myTableView];
    [self creatData];
    
    [self.myTableView addLegendHeaderWithRefreshingBlock:^{
        [self.myTableView.header endRefreshing];
    }];
    [self.myTableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(creatData)];
    
    
}

- (void)creatData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://open3.bantangapp.com/community/post/editorRec?app_installtime=1440915950.090587&app_versions=4.1.1&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&oauth_token=6de3bace25123737e17db3e839477eb1&os_versions=8.2&page=%ld&pagesize=10&screensize=640&track_device_info=iPhone&track_deviceid=B4391E1B-981F-4473-A960-431F69E76E8C&track_user_id=1260331&v=6",(long)_footer];
    [AFNBlock getDataWithString:urlStr block:^(id block) {
        NSDictionary *dataDic = [block objectForKey:@"data"];
        if (self.bScrollArr.count == 0) {
        for (NSDictionary *dic in [dataDic objectForKey:@"banner"]) {
            BScrollModel *model = [BScrollModel modelWithDictionary:dic];
            [_bScrollArr addObject:model];
            }
        }
        if (self.sScrollArr.count == 0) {
        for (NSDictionary *dic in [dataDic objectForKey:@"subject"]) {
            SScrollModel *model = [SScrollModel modelWithDictionary:dic];
            
                [_sScrollArr addObject:model];
            }
            
        }
        
        
        for (NSDictionary *dic in [dataDic objectForKey:@"list"]) {
            FindTableModel *model = [FindTableModel modelWithDictionary:dic];
            model.url = [[[dic objectForKey:@"pics"] objectAtIndex:0] objectForKey:@"url"];
            model.likes = [[dic objectForKey:@"dynamic"] objectForKey:@"likes"];
            model.username = [[dic objectForKey:@"author"] objectForKey:@"username"];
            model.avatar = [[dic objectForKey:@"author"] objectForKey:@"avatar"];
            [_tableArr addObject:model];
            }
        
        
        if (!self.headerView) {
            [self headerViewCreate];
        }
        
        [_myTableView reloadData];
        [self.myTableView.footer endRefreshing];
        self.footer++;
    }];

}

- (void)headerViewCreate
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_WIDTH)];
    _myTableView.tableHeaderView = self.headerView;
    [_headerView release];
    
    
    //上面的大scrollView
    _bScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*3/5)];
    _bScrollView.contentSize = CGSizeMake(_bScrollArr.count*SCREEN_WIDTH, 0);
    _bScrollView.contentOffset = CGPointMake(0, 0);
    _bScrollView.pagingEnabled = YES;
    _bScrollView.scrollEnabled = NO;
    _bScrollView.showsHorizontalScrollIndicator = NO;
    _bScrollView.delegate = self;
    [_headerView addSubview:_bScrollView];
    [_bScrollView release];
    
    
    for (int i = 0; i < _bScrollArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_WIDTH*3/5)];
        BScrollModel *model = [_bScrollArr objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        imageView.userInteractionEnabled = YES;
        [_bScrollView addSubview:imageView];
        [imageView release];
        
        UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe)];
        leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
        [imageView addGestureRecognizer:leftSwipe];
        [leftSwipe release];
        
        UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe)];
        rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
        [imageView addGestureRecognizer:rightSwipe];
        [rightSwipe release];
    }
    

    //下面的小scrollView
    _sScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SCREEN_WIDTH*3/5, SCREEN_WIDTH, SCREEN_WIDTH*2/5)];
    _sScrollView.contentSize = CGSizeMake(_sScrollArr.count*(SCREEN_WIDTH*12/35), 0);
    _sScrollView.showsHorizontalScrollIndicator = NO;
    [_headerView addSubview:_sScrollView];
    [_sScrollView release];
    
    for (int i = 0; i < _sScrollArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*2/35*i+10+SCREEN_WIDTH*2*i/7, SCREEN_WIDTH*2/35, SCREEN_WIDTH*2/7, SCREEN_WIDTH*2/7)];
        SScrollModel *model = [_sScrollArr objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.pic1]];
        imageView.userInteractionEnabled = YES;
        [_sScrollView addSubview:imageView];
        [imageView release];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self action:@selector(sClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, 0, SCREEN_WIDTH*2/7, SCREEN_WIDTH*2/7);
        [imageView addSubview:button];
    }
}

- (void)bClick:(UIButton *)sender
{
    NSLog(@"lll");
}

- (void)sClick:(UIButton *)sender
{
    NSLog(@"555");
}

- (void)leftSwipe
{
    NSLog(@"left");
    CATransition *transition = [CATransition animation];
    //动画效果
    transition.type = @"cube";
    //动画方向
    [transition setSubtype:kCATransitionFromRight];
    //动画时长
    transition.duration = 1;
    //重复次数(无限)
    // transition.repeatCount = NSIntegerMax;
    [_bScrollView.layer addAnimation:transition forKey:@"transition"];
    NSInteger a ;
    a = self.bScrollView.contentOffset.x/SCREEN_WIDTH;
    a++;
    if (a > self.bScrollArr.count-1) {
        a = 0;
    }
    NSLog(@"%ld",(long)a);
    self.bScrollView.contentOffset = CGPointMake(a*SCREEN_WIDTH, 0);
}

- (void)rightSwipe
{
    NSLog(@"right");
    CATransition *transition = [CATransition animation];
    //动画效果
    transition.type = @"cube";
    //动画方向
    [transition setSubtype:kCATransitionFromLeft];
    //动画时长
    transition.duration = 1;
    //重复次数(无限)
    // transition.repeatCount = NSIntegerMax;
    [_bScrollView.layer addAnimation:transition forKey:@"transition"];
    NSInteger b;
    b = self.bScrollView.contentOffset.x/SCREEN_WIDTH;
    b--;
    if (b < 0) {
        b = self.bScrollArr.count-1;
    }
    NSLog(@"%ld",(long)b);
    _bScrollView.contentOffset = CGPointMake(b*SCREEN_WIDTH, 0);
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH*31/20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cell";
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[FindTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.contentView.backgroundColor = [NightHandle shareHandle].viewColor;
    cell.userLabel.textColor = [NightHandle shareHandle].wordColor;
    cell.timeLabel.textColor = [NightHandle shareHandle].wordColor;
    cell.contentLabel.textColor = [NightHandle shareHandle].wordColor;
    FindTableModel *model = [_tableArr objectAtIndex:indexPath.row];
    [cell getCell:model];
//    [cell.shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [cell.commentButton addTarget:self action:@selector(comment:) forControlEvents:UIControlEventTouchUpInside];
    cell.commentButton.tag = indexPath.row+1000;
    [cell.likeButton addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentViewController *comment = [[CommentViewController alloc] init];
    FindTableModel *model = [self.tableArr objectAtIndex:indexPath.row];
    comment.urlStr = model.share_url;
    [self.navigationController pushViewController:comment animated:YES];
}


- (void)comment:(UIButton *)sender
{
    CommentViewController *comment = [[CommentViewController alloc] init];
    FindTableModel *model = [self.tableArr objectAtIndex:sender.tag-1000];
    comment.urlStr = model.share_url;
    [self.navigationController pushViewController:comment animated:YES];
}

- (void)like:(UIButton *)sender
{
    NSLog(@"like");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
